/**
 * 
 */
package library;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.omg.CORBA.FREE_MEM;

import user_client.LibrarianUserClient;
import user_client.NormalUserClient;
import user_gui.LibrarianUserGUI;
import user_gui.NormalUserGUI;

/**
 * @author Paweł Rychły
 * 
 */
public class LibraryMainFrame extends JFrame {

	private JPanel westPanel = new JPanel();
	private JPanel centerPanel = new JPanel();
	private Connection connection;

	public LibraryMainFrame() {

		LoginPane login = new LoginPane(this);
		this.westPanel.setLayout(new GridLayout(4, 1));
		this.westPanel.add(login);
		this.add(westPanel, BorderLayout.WEST);
		this.add(centerPanel, BorderLayout.CENTER);
	}

	/**
	 * Sets the connection variable.
	 * 
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public void setConnection(Connection connection, String userName,
			String userPass) throws SQLException {

		if ((this.connection != null) && (!this.connection.isClosed())) {
			this.connection.close();
			System.out.println("LibraryMainFrame:  connectionClosed!!!");
		}
		this.connection = connection;
		System.out.println("LibraryMAinFrame:  setConnection!!!");
		Statement stmt;
		try {
			stmt = this.connection.createStatement();
			ResultSet rs = stmt
					.executeQuery("select distinct granted_role from user_role_privs");

			String userRole = "";
			System.out.println("LibraryMAinFrame:  Role");
			
			while (rs.next()) {
				String role = rs.getString("granted_role");
				if (role.equalsIgnoreCase("LibrarianUserClient")) {
					userRole = "Librarian";
				}

				if ((!userRole.equalsIgnoreCase("Librarian"))
						&& (role.equalsIgnoreCase("UserClient"))) {
					userRole = "userClient";
				}
			}

			
			System.out.println("mainFrame: " + userRole);
			if (userRole.equalsIgnoreCase("Librarian")) {
				System.out.println("LibrarianUserClient");
				this.remove(this.centerPanel);
				this.centerPanel = new LibrarianUserGUI(
						new LibrarianUserClient(this.connection, userName,
								userPass));
				this.add(centerPanel);
				this.revalidate();
			} else if (userRole.equalsIgnoreCase("userClient")) {
				System.out.println("UserClient");
				this.remove(this.centerPanel);
				this.centerPanel = new NormalUserGUI(new NormalUserClient(
						this.connection, userName, userPass));
				this.add(centerPanel);
				this.revalidate();
				centerPanel.repaint();

			} else {

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
